package com.isunland.managesystem.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.MyUtils;
import java.io.Serializable;

/**
 * 对象功能:  基类fragment
 * 说明:自定义行为,可以设置标题.设置返回.
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/2/24 13:25
 */

@SuppressWarnings("unused") public class BaseFragment extends Fragment {
  public static final String EXTRA_PARAMS = BaseFragment.class.getSimpleName() + ".EXTRA_PARAMS";
  public BaseVolleyActivity mActivity;
  public CurrentUser mCurrentUser;
  public BaseParams mBaseParams;

  public static Fragment newInstance(BaseParams params, Fragment fragment) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_PARAMS, params);
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (BaseVolleyActivity) getActivity();
    mCurrentUser = CurrentUser.newInstance(mActivity);
    setHasOptionsMenu(true);
    setBack(true);//默认有返回按钮,因为大部分界面都有
    initData();
    if (MyUtils.isMock()) {
      initMockData();
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    if (initLayoutId() != 0) {
      View view = inflater.inflate(initLayoutId(), container, false);
      ButterKnife.bind(this, view);
      return view;
    } else {
      return super.onCreateView(inflater, container, savedInstanceState);
    }
  }

  /**
   * 初始化layoutId,如果为0,代表默认
   *
   * @return 布局id.
   */
  protected int initLayoutId() {
    return 0;
  }

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initView(view, savedInstanceState);
  }

  public void initView(View view, Bundle savedInstanceState) {
    //因为子类可以复写父类的方法,所以initData行为发生改变.mBaseParams可能没有初始化,需要做非空判断
    setTitleCustom(mBaseParams == null ? null : mBaseParams.getTitle());
  }

  public void initData() {
    if (getArguments() != null) {
      Serializable ser = getArguments().getSerializable(EXTRA_PARAMS);
      if (ser != null && ser instanceof BaseParams) {
        mBaseParams = (BaseParams) ser;
      }
    }
    if (mBaseParams == null) {

      mBaseParams = new BaseParams();
    }
  }

  public void initMockData() {

  }

  public void setTitleCustom(String title) {

    ActionBar actionBar = mActivity.getSupportActionBar();
    if ((actionBar != null) && (!TextUtils.isEmpty(title))) {
      actionBar.setTitle(title);
    }
  }

  public void setTitleCustom(int title) {
    if (mActivity == null) return;
    ActionBar actionBar = mActivity.getSupportActionBar();
    if ((actionBar != null) && (!TextUtils.isEmpty(getResources().getString(title)))) {
      actionBar.setTitle(title);
    }
  }

  public void setBack(boolean hasBack) {

    ActionBar actionBar = mActivity.getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(hasBack);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮
      case android.R.id.home:
        mActivity.finish();
        //return true,上一个Activity不会重新创建.
        return true;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public void showDialog(DialogFragment dialog, int requestCode, String tag) {
    if (dialog == null) return;
    if (tag == null) tag = "";
    //当activity不再前台时,会报错.java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    try {
      FragmentManager fm = mActivity.getSupportFragmentManager();
      //dialog不重复显示.通过tag区分,相同的tag的话,不再显示.
      dialog =
          ((!MyStringUtil.isEmpty(tag)) && (fm.findFragmentByTag(tag) instanceof DialogFragment))
              ? (DialogFragment) fm.findFragmentByTag(tag) : dialog;
      if (dialog.isVisible()) return;
      dialog.setTargetFragment(this, requestCode);
      dialog.show(fm, tag);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void showDialog(DialogFragment dialog, int requestCode) {
    showDialog(dialog, requestCode, "");
  }

  public void showDialog(DialogFragment dialog) {
    showDialog(dialog, 0, "");
  }

  private void dismissDialog(String tag, Context context) {
    if (context == null) return;
    if (tag == null) tag = "";
    try {
      FragmentManager fm = mActivity.getSupportFragmentManager();
      Fragment dialog = fm.findFragmentByTag(tag);
      if (dialog instanceof DialogFragment) ((DialogFragment) dialog).dismiss();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
